import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import Editor from '../index';

import Apis from '../../../../api';
import { Post } from '../../../../types';

export default function PagePostUpdator() {
  const { postId } = useParams<{ postId: string }>();
  const [post, setPost] = useState<undefined | Post>();

  useEffect(() => {
    Apis.Post.getPost(+postId)
      .then((it) => setPost(it))
      .catch((err) => console.error(err));
  }, [postId]);

  if (!post) {
    return <div>加载中...</div>;
  } else {
    return <Editor title={post.title} postId={post.postId} content={post.content} />;
  }
}
